<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/9/29
 * Time: 18:41
 */
namespace app\order\controller;

use think\Lang;
class ModelSupply extends Base{

    /**
     * 客供料执行
     */
    public function index(){
        $field = 'mod_sup_id,state,client,model_type,sup_type,provider,product_status,fitid,order_qty,sale_id';
        $where = array();
        $modelSupplies = $this->modelSupplies()->page($field, $where);
        $modelSupplies = $this->resets($modelSupplies);
        $this->page($modelSupplies);
        $this->asgModelTypes();
        $this->assign('modelSupplies', $modelSupplies);
        return $this->fetch('modsup/index');
    }

    /**
     * 修改action
     */
    public function esave(){
        $modSupId = input('param.mod_sup_id');
        $where = array('mod_sup_id'=>$modSupId);
        $find = $this->modelSupplies()->findById($where);
        if(!$find){
            return $this->error(Lang::get('unfind supply'));
        }
        $datas = input('post.');
        $edit = $this->modelSupplies()->update($datas, $where);
        if(!$edit){
            return $this->error(Lang::get('edit fail'));
        }
        return $this->success(Lang::get('edit success'), 'ModelSupply/index');
    }

    /**
     * json
     */
    public function one(){
        $modSupId = input('param.mod_sup_id');
        $find = $this->modelSupplies()->find(array('mod_sup_id'=>$modSupId));
        $find = $this->resetOne($find);
        echo json_encode($find);
    }

    /**
     * 搜索action
     */
    public function search(){
        $search = input('param.product_status');
        $where = array();
        if($search != ''){
            $where['product_status'] = $search;
        }
        $saleId = session('order_saleid');
        if($saleId != 0){
            $where['ari_orders.sale_id'] = $saleId;
        }
        $supplies = $this->modelsupplies()->search($where);
//        var_dump($supplies);exit();
        $supplies = $this->resets($supplies);
        $this->page($supplies);
        $this->asgModelTypes();
        $this->assign('modelSupplies', $supplies);
        return $this->fetch('modsup/index');
    }


    /**
     * 导出客供料执行列表--20190515
     */
    public function exportSupply(){
        $supplies = $this->modelsupplies()->joinSelect();
        $supplies = $this->resets($supplies);
        $excel = new ExportExcel();
        return $excel->exportSupply($supplies);
    }

    /**
     * 重组
     * @param $modelSupplies
     * @return mixed
     */
    private function resets($modelSupplies){
        $len = count($modelSupplies);
        if($len > 0){
            for($i=0;$i<$len;$i++){
                $modelSupplies[$i] = $this->resetOne($modelSupplies[$i]);
            }
        }
        return $modelSupplies;
    }
    private function resetOne($modelSupply){
        $len = count($modelSupply);
        if($len > 0){
            for($i=0;$i<$len;$i++){
                $fid = $modelSupply['fitid'];
                $findFid = $this->fittings()->findById(array('fid'=>$fid));
                if($findFid){
                    $modelSupply['fitting'] = $findFid['fitting'];
                }else{
                    $modelSupply['fitting'] = '';
                }
                /*$fid = $this->stringToArr($modelSupply['fitid']);
                $fidLen = count($fid);
                $modelSupply['fitting'] = '';
                if($fidLen > 0){
                    $where = array('fid'=>['in', $fid]);
                    $findFid = $this->fittings()->select('fid,fitting', $where);
                    if($findFid){
                        for($j=0;$j<count($findFid);$j++){
                            $modelSupply['fitting'] .= $findFid[$j]['fitting'].',';
                        }

                    }
                }*/
            }
        }else{
            $modelSupply['fitting'] = '';
        }
        return $modelSupply;
    }

}